package com.fast.develop.integration.web;

import com.fast.develop.core.data.Page;
import com.fast.develop.core.data.PageRequest;
import com.fast.develop.core.persistence.DomainModelManager;
import com.fast.develop.core.vo.PageReq;
import com.fast.develop.core.vo.ResourcesVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Map;

/**
 * @author 莫寅
 * @createTime 2022-06-05 04:08
 * @desc：
 */

public class DomainModelResourcesRest {

    private DomainModelManager domainModelManager;

    public DomainModelResourcesRest(DomainModelManager domainModelManager) {
        this.domainModelManager = domainModelManager;
    }

    @GetMapping("/{model}")
    public ResponseEntity<ResourcesVo> resources(@PathVariable(name = "model") String model, PageReq pageReq){
        PageRequest pageRequest = PageRequest.of(pageReq.getPage(), pageReq.getSize());

        Page page = domainModelManager.page(model, pageRequest);

        ResourcesVo pageVo = new ResourcesVo(page);
        return new ResponseEntity(pageVo, HttpStatus.OK);
    }

    @PostMapping("/{model}")
    public ResponseEntity create(@PathVariable(name = "model") String model, @RequestBody Map map){
        Serializable serializable = domainModelManager.create(model, map);
        return new ResponseEntity(serializable, HttpStatus.OK);
    }

    @PutMapping("/{model}/{id}")
    public ResponseEntity update(@PathVariable(name = "model") String model, @PathVariable(name = "id") String id, @RequestBody Map map){
        domainModelManager.update(model, id, map);
        return new ResponseEntity(HttpStatus.OK);
    }


    @GetMapping("/{model}/{id}")
    public ResponseEntity get(@PathVariable(name = "model") String model, @PathVariable(name = "id") String id){
        return new ResponseEntity(domainModelManager.get(model, id), HttpStatus.OK);
    }


    @DeleteMapping("/{model}/{id}")
    public ResponseEntity delete(@PathVariable(name = "model") String model, @PathVariable(name = "id") String id){
        domainModelManager.delete(model, id);
        return new ResponseEntity(HttpStatus.OK);
    }


    public DomainModelManager getdomainModelManager() {
        return domainModelManager;
    }
}
